﻿Public Class InputControl

    Dim timeleft As Integer
    Dim totaltime As Integer
    Dim hastimelimit As Boolean = False
    Event AnswerGiven(ByVal Answer As String)
    Event OutOfTime()

    Sub New()
        InitializeComponent()
    End Sub

    Const QuestionNumberStart = "Question Number {0}"

    Sub NewQuestion(ByVal Question As MPQService.Question)
        Me.QuestionNumber.Text = System.String.Format(QuestionNumberStart, Question.QuestionID)
        Me.QuestionText.Text = Question.QuestionText
        If Question.TimeLimit = 0 Then
            hastimelimit = False
            TimeRemaining.Visible = False
            TimeRemainingLabel.Visible = False
        Else
            hastimelimit = True
            timeleft = Question.TimeLimit * 10
            totaltime = Question.TimeLimit * 10
            TimeRemaining.Visible = True
            TimeRemainingLabel.Visible = True
            TimeRemainingLabel.BringToFront()
        End If
    End Sub

    Private Sub SecondTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SecondTimer.Tick
        TimeRemaining.Value = timeleft / totaltime * 100
        TimeRemainingLabel.Text = "Time Remaining: " & CInt(timeleft / 10)
        timeleft -= 1
        If timeleft <= -1 Then
            SecondTimer.Stop()
            RaiseEvent OutOfTime()
        End If
    End Sub

    Public Sub Start()
        SecondTimer.Start()
    End Sub

    Private Sub MultichoiceControl_CorrectAnswerEvent() Handles Me.AnswerGiven
        SecondTimer.Stop()
    End Sub

    Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
        RaiseEvent AnswerGiven(InputAnswer.Text)
    End Sub

    Private Sub InputAnswer_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles InputAnswer.KeyPress
        If e.KeyChar = vbCr Then
            Submit.PerformClick()
        End If
    End Sub
End Class
